package com.tyj.movie.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期: 2021/7/13 18:44
 * 作者: 谭友杰
 * 类说明:
 */

public class DateUtil {
    /**
     * 获取日期间隔 0是今天 1是明天 2是后天 ...也有负数
     *
     * @param startDate
     * @return
     */
    public static Long getDayString(Date startDate) {
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (startDate.getTime() + offSet) / 86400000;
        return (start - today);
    }

    public static String getWeek(Date date) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 获得时间差
     * @param d1
     * @param d2
     * @return 秒
     */
    public static Long getSecond(Date d1,Date d2){
        Long cha =d1.getTime()-d2.getTime();
        return cha/1000;
    }

    /**
     * 得到日期格式  今天 1月1日 12:10
     * @return
     */
    public static String getDateFormat(Date date){
        String str = "";
        if (DateUtil.getDayString(date) == 0) {
            str = "今天";
        } else if (DateUtil.getDayString(date) == 1) {
            str = "明天";
        } else if (DateUtil.getDayString(date) == 2) {
            str = "后天";
        } else if (DateUtil.getDayString(date) >= 3) {
            str = DateUtil.getWeek(date);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
        String format = simpleDateFormat.format(date);
        return str+format;

    }
}
